<?php
/**
 * Front
 * @filesource
 */
/**
 * Namespace
 */
namespace framework;
/**
 * Dependencies
 */
use framework\library\Registry;
/**
 * Plurious Framework Front Controller
 * 
 * Extends Registry. 
 * 
 * 'IS A' registry for the entire framework
 * 
 * Singleton: use framework\Front::getInstance();
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses Registry (extends - therefore required)
 * @uses framework\library\Config
 * @uses framework\engine\Action
 * @uses framework\library\Document
 * @uses framework\library\Request
 * @uses framework\library\Cache (optional)
 * @uses framework\library\access_control\User (optional)
 * @uses framework\library\Session (optional, but required if User is used)
 * @uses framework\helper\Url  (optional)
 */
class Front extends Registry {
    /** 
     * Contains singleton instance 
     * @var object $instance 
     */
    private static $instance;

    /** 
     * Holds the core classes required at launch 
     * @var array $baseClasses 
     */
    protected static $baseClasses = array(
        'framework\\library\\Config',
        'framework\\library\\Document',
        'framework\\library\\Request',
        'framework\\engine\\Actions',
        'framework\\library\\Cache',
        'framework\\library\\Session',
        'framework\\library\\access_control\\User',
        'framework\\helper\\Url'
    );

    /**
     * Private constructor used at initialization only
     */
    private function __construct() {
        $this::$instance = $this;
        $this->init();
    }

    /**
     * Invoked only at initial launch - starts the entire process
     * 
     * Uses the route obtained from the $_GET superglobal.  Route must be in
     * the form [context/]controller[/method/arg/arg/...] where brackets 
     * surround optional portions.
     * 
     * @uses Actions
     * @uses Request
     */
    private function init() {
        $this->loadBase(self::$baseClasses);
        $actions = $this->get('actions');
        $request = $this->get('request');
        $actions->run($request->getGet('route'));
    }

    /**
     * Retrieve instance of singleton
     * 
     * @return Object Instance of Front
     */
    public static function getInstance() {
        if (!isset(self::$instance)) {
            $className = __CLASS__;
            self::$instance = new $className;
        }
        return self::$instance;
    }

    /**
     * Cannot clone a singleton
     */
    public function __clone() {
        throw new \InvalidArgumentException('Clone is not allowed.');
    }
    

}
